/*个人订单列表*/
require(['jquery','../modules/pageSize','../modules/popup', 'jqScroller'], function($,pageSetSize,popup){
	//设置html根目录字体大小
	setRootFontSize(pageSetSize);
    var dyHeadList = {"$3" : "未支付", "$0" : "待发货", "$10" : "待收货", "$8" : "待评价" };
	var _isAjaxLoad = false;
	var curPagePos = 1;
    $('.category-ui li').on('click',function(e){
		e.preventDefault();
        if(!$(this).hasClass('active')){
			$(this).addClass('active').siblings().removeClass('active');
			curPagePos = 1;
			curAjaxModeId = $(this).data("oid");
			jqScroller.reset();
			jqScroller.block();
			jqScroller.render(curPagePos);
		}
    });
	/*	
	$(".progress-loader a").on("click", function(e){
		e.preventDefault();
		if(_isAjaxLoad) return;
		curPagePos++;
		ajaxMemberOrder(curAjaxModeId);
	});
	*/
	jqScroller.render = function(cpage){
		popup.loadPopup("正在加载中...");
		$.ajax({
            type: "GET",
            url: "/Home/Member/memberOrderInterface"+getFixedExtraUrl(),
            data: {"view" : curAjaxModeId, "page" : cpage},
            dataType: "json",
            success: function (rs) {
                popup.removeLoad();
				if(rs && rs.data != null){					
                    renderMemberCommon(rs.data, curAjaxModeId);
					curPagePos++;
					jqScroller.curPage++;
					jqScroller.unblock();
                }else{
					jqScroller.done();
					if(curPagePos == 1){
						$(".member-order-list").html("<div class='no-data-txt'>没有数据！</div>");	
					}else{						
						popup.alertPopup("没有更多数据！");
					}                    
                }                
            },
            error: function () {
				popup.removeLoad();
                popup.alertPopup('服务器异常，请稍后重试！');
                jqScroller.unblock();
            }
        });
    };
    jqScroller.run();
    jqScroller.render(1);
	function getMOurl(url, opts){
		var params = "";	
		for(var k in opts){
			params += "/" + k + "/" + opts[k];	
		}
		return url + params + getFixedExtraUrl();
	}
	function renderMemberCommon(rs, oid){
		var cur;
		var cgoods;
		var pay_status, ship_status, received_status, refund_status, comment_status, status, sell_ago, sell_front, re_type, rem_status, g, drawback_status, curBackNum;
		var notAvailable;
        var output = '';

		// drawback_status 0:无退款 1:退款失败 2:部分退款 3:全部退款
		for(var i = 0;i < rs.length;i++){
			cur = rs[i];
            output += '<div class="member-order-ycone">';
			output += '<div class="member-order-header">';
			output += '<div class="shop-choice"><i><img src="/Public/Home/images/member-shop-icon.png"></i><span>&nbsp;&nbsp;'+cur.local_name+'</span></div>';
            if(oid == 6 || oid == 7){
                output += '<span class="member-order-state">'+cur.order_msg+'</span>';
            }else{
                output += '<span class="member-order-state">'+dyHeadList["$"+oid]+'</span>';
            }

			output += '</div>';
			
			output += '<div class="member-order-contant">';
			cgoods = cur.item_order;
			drawback_status = 0;
			curBackNum = 0;
            output += '<div class="single-item dataItem">';
			for(var j = 0;j < cgoods.length;j++){
                g = cgoods[j];				
				re_type = parseInt(g.re_type);
				rem_status = parseInt(g.rem_status);
                output += '<div class="swipe-item">';
				output += '<a href="/'+getMOurl('Member/memberOredrData', {order_id: cur.order_id})+'" class="img"><img src="'+getDefaultImg(g.m_url, true)+'" alt="" /></a>';
				output += '<div class="goods-text">';	
				output += '<a href="/'+getMOurl('Member/memberOredrData', {order_id: cur.order_id})+'" class="name">'+g.name+'</a>';
				output += '<p class="pninfo"><span class="price">¥'+g.price+'</span><span class="num">&nbsp;X&nbsp;'+g.nums+'</span></p>';
				output += '</div>';
				if(re_type != -1){
					if(rem_status == 0){
						output += '<div class="refund-tips">审核未通过</div>';
						drawback_status = 1;
					}else if(rem_status == 5){
						output += '<div class="refund-tips">退款成功</div>';
						curBackNum++;
					}else{
						output += '<div class="refund-tips">退款中</div>';
						curBackNum++;
					}
				}
				output += '</div>';
			}
			if(curBackNum == cgoods.length){
				drawback_status = 3;
			}else if(curBackNum > 0){
				drawback_status = 2;
			}
            output += '<div class="swipe-price"><div class="swipe-price-data">';
            output += '<span>共计'+cgoods.length+'件商品</span>';
            output += '<span>实际支付：￥'+cur.total_amount+'</span>';
            output += '</div></div>';
            output += '</div>';
			pay_status = parseInt(cur.pay_status);
			ship_status = parseInt(cur.ship_status);
			received_status = parseInt(cur.received_status);
			comment_status = parseInt(cur.comment_status);
            notAvailable =  cur.status;
			output += '<div class="member-order-operate"><div class="operate-position">';
            //未支付
			if(notAvailable == 'dead' || notAvailable == 'finish'){//失效
				output += '<a href="javascript:;" onclick="opreateOrder(this,\''+cur.order_id+'\',3)"><div class="delete-order operate-public current">删除订单</div></a>';
			}else if(pay_status == 0){
				output += '<a href="javascript:;" onclick="opreateOrder(this,\''+cur.order_id+'\',1)"><div class="cancel-order operate-public current">取消订单</div></a>';
				output += '<a href="/'+getMOurl('Member/payOrder', {order_id: cur.order_id})+'"><div class="payment operate-public current">去付款</div></a>';
				output += '<div class="next-step" style="display:none;">';
				output += '<a href="javascript:;" onclick="opreateOrder(this,\''+cur.order_id+'\',3)"><div class="delete-order operate-public current">删除订单</div></a>';
				output += '</div>';
			}else if(ship_status == 0){//未发货
				output += '<a href="/'+getMOurl('MemberRefund/index', {order_id: cur.order_id, sell : 'refund_front_status'})+'"><div class="reminder operate-public current">'
                + ((drawback_status  > 0  && drawback_status != 1)  ? "退款详情" : "退款")+'</div></a>';
				if(drawback_status != 3){
					output += '<a href="javascript:;" onclick="askOrder(\''+cur.tel+'\')"><div class="reminder operate-public current">催单</div></a>';
				}
			}else if(received_status == 0){//未收货
				if(cur.logi_name !== "怡臣速送"){
					output += '<a href="https://m.kuaidi100.com/index_all.html?type='+cur.logi_name+'&postid='+cur.logi_no+'&callbackurl='+kd_pre_url+'/member/memberOrder/view/'+oid+getFixedExtraUrl()+'"><div class="logistics operate-public current">查看物流</div></a>';
				}else{
					output += '<a href="/'+getMOurl('Member/delivery', {order_id: cur.order_id})+'"><div class="logistics operate-public current">查看物流</div></a>';
				}
				output += '<a href="/'+getMOurl('Member/afterSale', {order_id: cur.order_id})+'"><div class="after-sale operate-public current">售后</div></a>';
				output += '<a href="javascript:;" onclick="opreateOrder(this,\''+cur.order_id+'\',2)"><div class="confirm-goods operate-public current active">确认收货</div></a>';
				output += '<div class="next-step" style="display:none;">';
				output += '<a href="/'+getMOurl('Member/afterSale', {order_id: cur.order_id})+'"><div class="after-sale operate-public current">售后</div></a>';
				output += '<a href="/'+getMOurl('Member/memberEvaluation', {order_id: cur.order_id})+'"><div class="evaluation operate-public current">评价</div></a>';
				output += '</div>';
			}else if(comment_status == 1){//未评价
				output += '<a href="/'+getMOurl('Member/afterSale', {order_id: cur.order_id})+'"><div class="after-sale operate-public current">售后</div></a>';
				output += '<a href="/'+getMOurl('Member/memberEvaluation', {order_id: cur.order_id})+'"><div class="evaluation operate-public current">评价</div></a>';
			}else if(comment_status == 2){//已经评价
				output += '<a href="/'+getMOurl('Member/afterSale', {order_id: cur.order_id})+'"><div class="after-sale operate-public current">售后</div></a>';
			}
			output += '</div></div></div>';
			output += '</div>';
            output += '</div>';
		}
		if(curPagePos % 10 == 0 || curPagePos == 1){
			$(".member-order-list").html(output);
		}else{
			$(".member-order-list").append(output);
		}
		
	}
});

